package com.tf.integrated.controller;

import com.ccssoft.m8.common.web.BaseController;
import com.tf.integrated.config.Config;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * 系统管理接口
 * @author allen
 * @date: 2020年9月14日上午10:56:20
 */
@Api(value = "系统管理接口",description="SystemController")
@RestController
@RequestMapping("/system")
public class SystemController extends BaseController
{
    @ApiOperation("重启系统")
    @PutMapping("/reboot")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public String reboot(@RequestParam Map<String, String> data)
    {
        if(data!=null&&data.size()>0)
        {
            String host = (String)data.get("host");
            HttpMethod method = HttpMethod.PUT;
            HttpHeaders headers = new HttpHeaders();
            headers.set("X-Token", Config.getmCurtoken(host));
            String analyseResults = Config.client(Config.getStarturlpre(host) + "/system/reboot", method, null, headers);
            logger.error("reboot on  system/reboot analyseResults===" + analyseResults);
            LogController.add("/system/reboot", LogController.METHOD_PUT, 1);
            return analyseResults;
        }
        LogController.add("/system/reboot", LogController.METHOD_PUT, 0);
        return "{}";
    }

    @ApiOperation("设备恢复默认配置")
    @PutMapping("/resetConfig")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public String resetConfig(@RequestParam Map<String, String> data)
    {
        if(data!=null&&data.size()>0)
        {
            String host = (String)data.get("host");
            HttpMethod method = HttpMethod.PUT;
            HttpHeaders headers = new HttpHeaders();
            headers.set("X-Token", Config.getmCurtoken(host));
            String analyseResults = Config.client(Config.getStarturlpre(host) + "/system/resetConfig" , method, null, headers);
            logger.error("resetConfig on  system/resetConfig analyseResults===" + analyseResults);
            LogController.add("/system/resetConfig",LogController.METHOD_PUT,1);
            return analyseResults;
        }
        LogController.add("/system/resetConfig",LogController.METHOD_PUT,0);
        return "{}";
    }

    @ApiOperation("设备恢复出厂默认")
    @PutMapping("/resetFactory")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public String resetFactory(@RequestParam Map<String, String> data)
    {
        if(data!=null&&data.size()>0)
        {
            String host = (String)data.get("host");
            HttpMethod method = HttpMethod.PUT;
            HttpHeaders headers = new HttpHeaders();
            headers.set("X-Token", Config.getmCurtoken(host));
            String analyseResults = Config.client(Config.getStarturlpre(host) + "/system/resetFactory" , method, null, headers);
            logger.error("resetFactory on  system/resetFactory analyseResults===" + analyseResults);
            LogController.add("/system/resetFactory",LogController.METHOD_PUT,1);
            return analyseResults;
        }
        LogController.add("/system/resetFactory",LogController.METHOD_PUT,0);
        return "{}";
    }

    @ApiOperation("设备关机")
    @PutMapping("/shutdown")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public String shutdown(@RequestParam Map<String, String> data)
    {
        if(data!=null&&data.size()>0)
        {
            String host = (String)data.get("host");
            HttpMethod method = HttpMethod.PUT;
            HttpHeaders headers = new HttpHeaders();
            headers.set("X-Token", Config.getmCurtoken(host));
            String analyseResults = Config.client(Config.getStarturlpre(host) + "/system/shutdown" , method, null, headers);
            logger.error("shutdown on  system/shutdown analyseResults===" + analyseResults);
            LogController.add("/system/shutdown",LogController.METHOD_PUT,1);
            return analyseResults;
        }
        LogController.add("/system/shutdown",LogController.METHOD_PUT,0);
        return "{}";
    }

    @ApiOperation("获取软件版本")
    @GetMapping("/softVersion")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public String softVersion(@RequestParam Map<String, String> data)
    {
        if(data!=null&&data.size()>0)
        {
            String host = (String)data.get("host");
            HttpMethod method = HttpMethod.GET;
            HttpHeaders headers = new HttpHeaders();
            headers.set("X-Token", Config.getmCurtoken(host));
            String analyseResults = Config.client(Config.getStarturlpre(host) + "/system/softVersion" , method, null, headers);
            logger.error("softVersion on  system/softVersion analyseResults===" + analyseResults);
            LogController.add("/system/softVersion",LogController.METHOD_GET,1);
            return analyseResults;
        }
        LogController.add("/system/softVersion",LogController.METHOD_GET,0);
        return "{}";
    }

    @ApiOperation("获取系统时间")
    @GetMapping("/time")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public String gettime(@RequestParam Map<String, String> data)
    {
        if(data!=null&&data.size()>0)
        {
            String host = (String)data.get("host");
            HttpMethod method = HttpMethod.GET;
            HttpHeaders headers = new HttpHeaders();
            headers.set("X-Token", Config.getmCurtoken(host));
            String analyseResults = Config.client(Config.getStarturlpre(host) + "/system/time" , method, null, headers);
            logger.error("gettime on  system/time analyseResults===" + analyseResults);
            LogController.add("/system/time",LogController.METHOD_GET,1);
            return analyseResults;
        }
        LogController.add("/system/time",LogController.METHOD_GET,0);
        return "{}";
    }

    @ApiOperation("设置系统时间")
    @PutMapping("/time")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public String settime(@RequestParam Map<String, String> data,@RequestBody String binfo)
    {
        if(data!=null&&data.size()>0)
        {
            String host = (String)data.get("host");
            HttpMethod method = HttpMethod.PUT;
            HttpHeaders headers = new HttpHeaders();
            headers.set("X-Token", Config.getmCurtoken(host));
            String analyseResults = Config.client(Config.getStarturlpre(host) + "/system/time" , method, binfo, headers);
            logger.error("settime on  system/time analyseResults===" + analyseResults);
            LogController.add("/system/time",LogController.METHOD_PUT,1);
            return analyseResults;
        }
        LogController.add("/system/time",LogController.METHOD_PUT,0);
        return "{}";
    }
}